package com.blog.admin.scheduled;

import com.blog.admin.service.FileInfoService;
import com.blog.admin.service.SiteConfigService;
import com.blog.common.core.constant.CommonConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static com.blog.common.core.constant.CacheConstants.FILE_INFO;

/**
 * <p>
 * 网站配置定时任务
 * </p>
 *
 * @author Valerie
 * @date 2023/6/3
 **/
@Component
@AllArgsConstructor
@Slf4j
public class SiteConfigTask {


    private final SiteConfigService siteConfigService;

    private final FileInfoService fileInfoService;

    /**
     * 定时刷新图片缓存
     *
     * @return void
     */
    @Scheduled(cron = "0 */30 * ? * *")
    @PostConstruct
    public void refreshFileInfoList(){
        fileInfoService.refreshFileInfoList(FILE_INFO);
    }


    /**
     * 定时获取二次元图片
     *
     * @return void
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void savePageCover(){
        final int count = 6;
        for (int i = 0; i < count; i++) {
            siteConfigService.savePageCover();
        }
        log.warn("定时任务->{}", "获取二次元图片");
    }

    /**
     * 定时设置网站主页背景
     *
     * @return void
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void setHomePageCover(){
        siteConfigService.setSitePageCover(CommonConstants.ORDER);

        log.warn("定时任务->{}", "设置网站背景");
    }

    /**
     * 定时设置网站其他页面背景
     *
     * @return void
     */
//    @Scheduled(cron = "0/20 * * * * ? ")
    public void setAnotherPageCover(){
        siteConfigService.setSiteAnotherPageCover(CommonConstants.ORDER);

        log.warn("定时任务->{}", "设置网站主页背景");
    }
}
